package com.caits.lbs.framework.utils;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.caits.lbs.framework.bean.common.Point;

public class PlaceUtils {

	/**
	 * 解析出行政区划描述信息
	 * 
	 * @param xmlresult
	 * @return
	 */
	private static String readRoles(String xmlresult) {
		try {
			Document doc = DocumentHelper.parseText(xmlresult);
			Element root = doc.getRootElement();
			String error = root.attributeValue("error");
			if (!error.equals("0"))
				return "";
			Element Add = root.element("Add");
			Element Dis = root.element("Dis");
			String v = "";
			String name = "";
			if (Add != null) {
				v = Add.attributeValue("v");
			}
			if (Dis != null) {
				name = Dis.attributeValue("name");
			}
			return name + ">" + v;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 行政区划查询，根据坐标点获取行政区和街道等具体地址
	 * 
	 * @param lon
	 *            Gps经度
	 * @param lat
	 *            Gps纬度
	 * @return
	 */
	public static String getSrteet(Double lon, Double lat) {
		String result = "等待实现";
		// try {
		// String xmlresult = StreetQuery.getSrteet(lon + "," + lat);
		// result = readRoles(xmlresult);
		// result = result.replaceAll(">", "");
		// } catch (UnsupportedEncodingException e) {
		// e.printStackTrace();
		// } catch (IOException e) {
		// e.printStackTrace();
		// } catch (JDOMException e) {
		// e.printStackTrace();
		// }
		//
		return result;
	}

	/**
	 * 计算两经纬度坐标间的大圆矩离 单位度
	 * 
	 * @param pt1
	 *            位置1
	 * @param pt2
	 *            位置2
	 * @return long 两点间的距离(米)
	 */
	public static long DistanceInGreatCircle(double lon1, double lat1, double lon2, double lat2) {
		// 将经纬度换算成度
		double x1, x2, y1, y2;
		x1 = lon1;
		y1 = lat1;
		x2 = lon2;
		y2 = lat2;
		double x = Math.cos(AngleToRadian(90 - y1)) * Math.cos(AngleToRadian(90 - y2)) + Math.sin(AngleToRadian(90 - y1)) * Math.sin(AngleToRadian(90 - y2))
				* Math.cos(AngleToRadian(x1 - x2));
		if (x > 0 && x > 1) {
			x = 0.99999;
		}
		if (x < 0 && x < -1) {
			x = -0.99999;
		}
		return (int) (GetEqualVolumeEarthRadius() * Math.acos(x));
	}

	/**
	 * 角度到弧度转换
	 * 
	 * @param Agnel
	 *            角度
	 * @return double 弧度值
	 */
	public static double AngleToRadian(double Angle) {

		return Angle * 3.14159265358979 / 180.0;
	}

	/**
	 * 取得等体积球体半径
	 * 
	 * @return
	 */
	public static double GetEqualVolumeEarthRadius() {
		double a = 6378245; // 克氏参数
		double b = 6356863;

		return Math.pow(a * a * b, 1.0 / 3.0);
	}

	/**
	 * 对点坐标进行加密处理--国家统一加密程序
	 * 
	 * @param x 单位度
	 * @param y 单位度
	 * @return  点对象，单位度
	 */
	public static Point encryPoint(double x, double y) {
		double x1, tempx;
		double y1, tempy;

		x1 = x * 3686400.0;
		y1 = y * 3686400.0;

		int gpsHeight = 0;
		int gpsWeek = 0;
		int gpsWeekTime = 0;

		double newx, newy;

		Converter converter = new Converter();

		Point point = converter.wgtochina_lb(1, (int) x1, (int) y1, gpsHeight, gpsWeek, gpsWeekTime);

		newx = point.getLatitude();
		newy = point.getLongitude();

		tempx = newx;
		tempy = newy;
		tempx = tempx / 3686400.0;
		tempy = tempy / 3686400.0;

		point = new Point();
		point.setLatitude(tempx);
		point.setLongitude(tempy);

		return point;
	}
}
